package system.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import core.exception.AccountingException;
import core.toolbox.formulaAnalysis.FormulaAnalysis;
import org.apache.shiro.SecurityUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/3/14.
 */
public class ReportInfoDao {

    public List<Record> getReportInfoByReportId(int reportId){

       return Db.find("select * from report_info where report_id = ?", reportId);
    }


    public BigDecimal getResultByformulaid(String formulaid, int period, int userid) {
        //获取公式里的每一项的信息
        List<Record> list = Db.find("select * from report_formula where formula_id = ?", formulaid);
        //循环计算结果
        BigDecimal result = new BigDecimal("0");
        for(Record item : list) {
            //根据datatype(取数规则)获取值
            BigDecimal value = FormulaAnalysis.getItemValueByDataType(item.getStr("subject_id"), item.getStr("data_type"), period, userid);
            String symbol = item.getStr("symbol");
            if("+".equals(symbol)||"＋".equals(symbol)){
                result = result.add(value);
            }else if("-".equals(symbol) || "－".equals(symbol)){
                result = result.subtract(value);
            }else{
                throw new AccountingException("未知符号["+symbol+"]");
            }
        }

        return result;
    }
}
